<template>
  <div>
    <el-divider> 群聊列表 </el-divider>
    <group-list :group-list="GroupList" @deleteGroup="getGroupList" />
    <el-divider> 拉机器人进群列表 </el-divider>
    <el-button type="primary" plain @click="botCountMessage">刷新状态</el-button>
    <apply-for-adding-group :apply-for-list="ApplyForList" @agreeGroup="agreeGroup" />
  </div>
</template>

<script>
import { groupList, countMessage } from '@/api/bot'

import GroupList from './ChildCpm/GroupList'
import ApplyForAddingGroup from './ChildCpm/ApplyForAddingGroup'
export default {
  components: {
    GroupList,
    ApplyForAddingGroup
  },
  data() {
    return {
      GroupList: [],
      ApplyForList: []
    }
  },
  created() {
    this.getGroupList()
    this.botCountMessage()
  },
  methods: {
    getGroupList() {
      groupList().then(res => {
        this.GroupList = res
      }).catch(err => {
        this.$message.error(err)
      })
    },
    botCountMessage() {
      countMessage().then(res => {
        this.ApplyForList = res.filter(item => {
          if (item.type !== 'BotInvitedJoinGroupRequestEvent') return false
          for (const i of this.GroupList) if (i.id === item.groupId) return false
          return true
        })
        if (this.ApplyForList.length === 0) this.$message({ type: 'warning', message: '暂无人拉我进群' })
      }).catch(err => {
        console.log(err)
      })
    },
    agreeGroup() {
      setTimeout(() => {
        this.getGroupList()
        setTimeout(() => {
          this.botCountMessage()
        }, 100)
      }, 100)
    }
  }
}
</script>

<style scoped>

</style>
